﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ServiceInterfaces.DataContracts;
using ServiceInterfaces.ServiceInterfaces;
using System.IO;

using BLL;

namespace ServiceImpl
{
    public class FilesService : IFilesLoad
    {
        OrderBLL _orderBLL = new OrderBLL();

        private Dictionary<int, List<ProductTempleteInfo>> _shopProductTempleteList = new Dictionary<int, List<ProductTempleteInfo>>();//商户产品列表
        //public List<TranFileInfo> GetFilesList()
        //{
        //    string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"];
        //    List<TranFileInfo> fileList = new List<TranFileInfo>();
        //    if (System.IO.Directory.Exists(filePath))
        //    {
        //        string[] files = Directory.GetFiles(filePath);
        //        foreach (string s in files)
        //        {
        //            var file = new FileInfo(s);
        //            fileList.Add(new TranFileInfo()
        //            {
        //                Name = file.Name,
        //                Length = file.Length,
        //                CreateTime = file.CreationTime
        //            });
        //        }
        //    }

        //    return fileList;

        //}

        /// <summary>
        /// 获取文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public TranFileInfo GetFiles(string filePath)
        {
            string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);
            if (File.Exists(filePath))
            {
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
                TranFileInfo file = new TranFileInfo();
                file.Name = fileName;
                file.Offset = fs.Length;
                fs.Close();
                return file;
            }
            return null;
        }

        /// <summary>
        /// 上传照片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public TranFileInfo UplodaFile(TranFileInfo file)
        {
            FileStream fs = null; 
            BinaryWriter writer =null;
            try
            {
                string filePath = file.DesFilePath;
                string filedir =file.DesFilePath.Substring(0,file.DesFilePath.LastIndexOf("\\"));
                if (!Directory.Exists(filedir))
                {
                    Directory.CreateDirectory(filedir);
                }
                 fs = new FileStream(filePath, FileMode.OpenOrCreate);//打开文件
                long offset = file.Offset;  //file.Offset 文件偏移位置,表示从这个位置开始进行后面的数据添加
                writer = new BinaryWriter(fs);//初始化文件写入器
                writer.Seek((int)offset, SeekOrigin.Begin);//设置文件的写入位置
                writer.Write(file.Data);//写入数据

                file.Offset = fs.Length;//返回追加数据后的文件位置
                file.Data = null;
            }
            catch (Exception ex)
            {
                file.ErorrMsg = ex.Message;
            }
            finally
            {
                writer.Close();
                fs.Close();
                fs.Dispose();
            }

            return file;
        }

        /// <summary>
        /// 根据产品Id修改产品以及照片状态
        /// </summary>
        /// <param name="OrderInfoId"></param>
        /// <returns></returns>
        public bool UpdateOrderMoveStatus(int OrderInfoId)
        {
            NewMovePhotoBLL _bll = new NewMovePhotoBLL();
            return _bll.UpdateOrderInfoMoveStatus(OrderInfoId);
        }

        /// <summary>
        /// 备份照片
        /// </summary>
        /// <param name="srcPath"></param>
        public string BuckUpPhoto(string srcPath)
        {
            string ret = string.Empty;
            try
            {
                CopyFileToBuckUp(srcPath, srcPath + "\\Backup");
            }
            catch(Exception ex)
            {
                ret = ex.Message;
            }
            return ret;
        }

        /// <summary>
        /// 将产品文件夹中所有照片移动到备份表中
        /// </summary>
        /// <param name="path"></param>
        private void CopyFileToBuckUp(string source,string destination)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(source);
            foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
            {
                if (fsi.Name == "Backup")
                    continue;
                string destName = Path.Combine(destination, fsi.Name);
                if (fsi is FileInfo)
                    File.Copy(fsi.FullName, destName);
                else
                {
                    Directory.CreateDirectory(destName);
                    CopyFileToBuckUp(fsi.FullName, destName);
                }
            }
            
        }

        /// <summary>
        /// 是否存在该crc
        /// </summary>
        /// <param name="RCR"></param>
        /// <returns></returns>
        public bool IsExsitCRC(int crc,out int repeatPhotoId)
        {
            return _orderBLL.IsExsitCRC(crc,out repeatPhotoId);
        }

        /// <summary>
        /// 添加已开单照片的crc
        /// </summary>
        /// <param name="RCR"></param>
        /// <returns></returns>
        public int AddCRC(int crc,int photoId)
        {
            return _orderBLL.AddCRC(crc,photoId);
        }

        /// <summary>
        /// 删除照片的crc
        /// </summary>
        /// <param name="rcr"></param>
        /// <returns></returns>
        public int DeleteCRC(int crc)
        {
            return _orderBLL.DeleteCRC(crc);
        }



    }
}
